package com.lxj.xianyu;

import com.alibaba.fastjson.JSON;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.List;
import java.util.Objects;

@Log
@Component
public class SpringDataPipeLine implements Pipeline {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public void process(ResultItems resultItems, Task task) {
        List<Item> items = resultItems.get("items");
        if (!CollectionUtils.isEmpty(items)) {
            for (Item item : items) {
                if (Objects.nonNull(item)) {
                    if (!redisTemplate.opsForSet().isMember("itemSet", item.getUrl())) {
                        redisTemplate.opsForSet().add("itemSet", item.getUrl());
                        redisTemplate.opsForList().leftPush(item.getKeyword(), JSON.toJSONString(item));
                    }
                }
            }
        }
    }
}
